import { createRouter, createWebHashHistory } from "vue-router";
import Store from "@/store";
import routes from "./routes";

const router = createRouter({
	history: createWebHashHistory(),
	routes,
	scrollBehavior() {
		// console.log(to);
		// console.log(from);
		// console.log(savedPosition);
		// 始终滚动到顶部
		return { top: 0 };
	},
});

router.beforeEach((to, from, next) => {
	window.$load?.start();
	const token: string | null = Store.getters["user/token"];
	if (to.meta.requiresAuth && !token) {
		next("/login");
	} else {
		next();
	}
});

router.afterEach(() => {
	window.$load?.finish();
});

export { useLink, useRoute, useRouter, RouterView } from "vue-router";

export type { RouteLocationRaw, RouteLocationMatched } from "vue-router";

export default router;
